package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

// 2.7.8
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    @Override
    public boolean importOrderSettings(List<String[]> readExcel) {
//        list 数据 批量录入 表 t_0rdersetting 里面mp
    List<OrderSetting>orderSettingList =changeTo(readExcel);
//    ordersetting 集合里面数据 数据库中是否存在 相关记录 如果存在
//        根据日期查询有无重复
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
           OrderSetting isOrderSettingEixt=  baseMapper.findOrderSettingByOrderDate(orderDate);
           if (isOrderSettingEixt!=null) {
               orderSetting.setId(isOrderSettingEixt.getId());
           }
        }

        boolean b = saveOrUpdateBatch(orderSettingList,100);
        return b;
    }

    private List<OrderSetting> changeTo(List<String[]> readExcel) {
        List<OrderSetting> orderSettingList = new ArrayList<>();
        if (readExcel!=null&&readExcel.size()!=0){
            for (String[] strings : readExcel) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setReservations(0);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0],"yyyy/MM/dd"));
                orderSettingList.add(orderSetting);
            }
        }
        return orderSettingList;
    }
    @Override
    public Map findSettingData(int year, int month) {
        //   根据 传递年和月  加载 预约数据   map   日期  yyyy-MM-dd      value  {number:xxx,reservations:xxx}
        String beginDate = year+"-"+month+"-1";
        String endDate = year+"-"+month+"-31";
        List<OrderSetting> orderSettingList = baseMapper.findSettingListByDate(beginDate,endDate);
        Map map = changeData(orderSettingList);
        return map;
    }

        @Override
    public void updateNumberByOrderDate(int number, String orderdate) {
        baseMapper.updateNumberByOrderDate(number,orderdate);
    }

    @Override
    public int isOrderOkByOrderDate(String orderdate) {
        return baseMapper.isOrderOkByOrderDate(orderdate);
    }

    @Override
    @LcnTransaction  //    事务发起者  起始方法上添加该注解
    public void updateReservationsByOrderDate(String orderdate) {
        baseMapper.updateReservationsByOrderDate(orderdate);
    }

    private Map changeData(List<OrderSetting> orderSettingList) {
        Map map  = new HashMap();
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            String date = DateUtils.parseDate2String(orderDate,"yyyy-MM-dd");
            map.put(date,orderSetting);
        }
        return  map;}
}
